package com.niji.littly_donkey_android.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;
import com.android.volley.*;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.niji.littly_donkey_android.R;
import com.niji.littly_donkey_android.adapter.BankCardAdapter;
import com.niji.littly_donkey_android.adapter.RelatedGoodsAdapter;
import com.niji.littly_donkey_android.adapter.TicketAdapter;
import com.niji.littly_donkey_android.bean.BankCard;
import com.niji.littly_donkey_android.bean.RelatedGoods;
import com.niji.littly_donkey_android.bean.Ticket;
import com.niji.littly_donkey_android.http.ConnectViaSession;
import com.niji.littly_donkey_android.util.EntityUtils;
import com.niji.littly_donkey_android.util.JsonUtil;
import com.niji.littly_donkey_android.util.UrlUtil;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RelatedGoodsActivity extends BaseActivity {

    private final static String TAG = "RelatedGoodsActivity";

    private Button related_goods_return;

    private ListView related_goods_list;


    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.related_goods_activity);
        relatedGoodsReturn();
        showRelatedGoodsList();
        updateDefault();
        searchScreen();

    }

    private void searchScreen() {
        ImageView related_goods_search = findViewById(R.id.related_goods_search);
        EditText related_goods_search_name = findViewById(R.id.related_goods_search_name);
        related_goods_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = related_goods_search_name.getText().toString().trim();
                if(!name.equals("")){
                    searchScreenByName(name);
                }else {
                    Toast.makeText(RelatedGoodsActivity.this,"请输入要搜索的景点名称",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


    /**
     * 通过名称搜索景点
     */
    private void searchScreenByName(String name) {
        related_goods_list = findViewById(R.id.related_goods_list);
        String url = UrlUtil.URL + "/attractions/system/list?page=1&per_page=100&name=" + name;
        ArrayList<RelatedGoods> relatedGoods = new ArrayList<>();
        RequestQueue requestQueue = Volley.newRequestQueue(RelatedGoodsActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    Map<String, Object> map = JsonUtil.jsonToMap(s);
                    Integer code = (Integer) map.get("code");
                    if (code == 0) {
                        Map<String,Object> data = (Map<String, Object>) map.get("data");
                        List<Map<String, Object>> mapList = (List<Map<String, Object>>) data.get("data");
                        Log.i(TAG, mapList.toString());
                        for (int i = 0; i < mapList.size(); i++) {
                            Integer id = (Integer) mapList.get(i).get("id");
                            String name = (String) mapList.get(i).get("name");
                            String uu_id = (String) mapList.get(i).get("uu_id");
                            String top_url = (String) mapList.get(i).get("top_url");
                            String price = (String) mapList.get(i).get("price");
                            String score = (String) mapList.get(i).get("score");
                            Integer comments = (Integer) mapList.get(i).get("comments");
                            String address = (String) mapList.get(i).get("address");
                            String latitude_longitude = (String) mapList.get(i).get("latitude_longitude");
                            String open_start = (String) mapList.get(i).get("open_start");
                            String open_end = (String) mapList.get(i).get("open_end");
                            String created_at = (String) mapList.get(i).get("created_at");
                            RelatedGoods relatedGood = new RelatedGoods();
                            relatedGood.setCity(address);
                            relatedGood.setId(id);
                            if (i == 0){
                                relatedGood.setDefault(true);
                            }else {
                                relatedGood.setDefault(false);
                            }
                            relatedGood.setName(name);
                            relatedGood.setPrice(price);
                            relatedGood.setComment(score);
                            relatedGood.setUrl(top_url);
                            relatedGoods.add(relatedGood);
                        }
                        related_goods_list.setAdapter(new RelatedGoodsAdapter(RelatedGoodsActivity.this,relatedGoods));
                    } else if (code == -1) {
                        Toast.makeText(RelatedGoodsActivity.this, "登录失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(RelatedGoodsActivity.this, GetPhoneCodeActivity.class);
                        intent.putExtra("loginCode", "-1");
                        startActivity(intent);
                        finish();
                    } else {
                        Object message = map.get("message");
                        Toast.makeText(RelatedGoodsActivity.this, message.toString(), Toast.LENGTH_SHORT).show();
                        overridePendingTransition(R.anim.xpage_slide_in_left, R.anim.xpage_slide_out_right);
                        finish();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(RelatedGoodsActivity.this, "网络请求失败", Toast.LENGTH_SHORT).show();
            }
        }) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                String sesseionid = new ConnectViaSession(RelatedGoodsActivity.this).GetSession();
                if (!sesseionid.equals("")) {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("cookie", sesseionid);
                    return headers;
                } else {
                    return super.getHeaders();
                }
            }
        };
        requestQueue.add(stringRequest);
    }


    private void updateDefault() {
        related_goods_list = findViewById(R.id.related_goods_list);
        Intent intent1 = getIntent();
        String className = intent1.getStringExtra("className");
        related_goods_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                System.out.println(position);
                ImageView item_related_goods_defalut = view.findViewById(R.id.item_related_goods_defalut);
                ImageView item_related_goods_not_dafalut = view.findViewById(R.id.item_related_goods_not_dafalut);
                if (className != null){
                    if(className.equals("ShootingReleaseActivity")){
                        Intent intent = new Intent(RelatedGoodsActivity.this,ShootingReleaseActivity.class);
                        intent.putExtra("attractions_id","1");
                        startActivity(intent);
                        finish();
                    }
                }
            }
        });
    }

    /**
     * 展示景点列表
     */
    private void showRelatedGoodsList() {
        related_goods_list = findViewById(R.id.related_goods_list);
        String url = UrlUtil.URL + "/attractions/system/list?page=1&per_page=100";
        ArrayList<RelatedGoods> relatedGoods = new ArrayList<>();
        RequestQueue requestQueue = Volley.newRequestQueue(RelatedGoodsActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    Map<String, Object> map = JsonUtil.jsonToMap(s);
                    Integer code = (Integer) map.get("code");
                    if (code == 0) {
                        Map<String,Object> data = (Map<String, Object>) map.get("data");
                        List<Map<String, Object>> mapList = (List<Map<String, Object>>) data.get("data");
                        Log.i(TAG, mapList.toString());
                        for (int i = 0; i < mapList.size(); i++) {
                            Integer id = (Integer) mapList.get(i).get("id");
                            String name = (String) mapList.get(i).get("name");
                            String uu_id = (String) mapList.get(i).get("uu_id");
                            String top_url = (String) mapList.get(i).get("top_url");
                            String price = (String) mapList.get(i).get("price");
                            String score = (String) mapList.get(i).get("score");
                            Integer comments = (Integer) mapList.get(i).get("comments");
                            String address = (String) mapList.get(i).get("address");
                            String latitude_longitude = (String) mapList.get(i).get("latitude_longitude");
                            String open_start = (String) mapList.get(i).get("open_start");
                            String open_end = (String) mapList.get(i).get("open_end");
                            String created_at = (String) mapList.get(i).get("created_at");
                            RelatedGoods relatedGood = new RelatedGoods();
                            relatedGood.setCity(address);
                            relatedGood.setId(id);
                            if (i == 0){
                                relatedGood.setDefault(true);
                            }else {
                                relatedGood.setDefault(false);
                            }
                            relatedGood.setName(name);
                            relatedGood.setPrice(price);
                            relatedGood.setComment(score);
                            relatedGood.setUrl(top_url);
                            relatedGoods.add(relatedGood);
                        }
                        related_goods_list.setAdapter(new RelatedGoodsAdapter(RelatedGoodsActivity.this,relatedGoods));
                    } else if (code == -1) {
                        Toast.makeText(RelatedGoodsActivity.this, "登录失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(RelatedGoodsActivity.this, GetPhoneCodeActivity.class);
                        intent.putExtra("loginCode", "-1");
                        startActivity(intent);
                        finish();
                    } else {
                        Object message = map.get("message");
                        Toast.makeText(RelatedGoodsActivity.this, message.toString(), Toast.LENGTH_SHORT).show();
                        overridePendingTransition(R.anim.xpage_slide_in_left, R.anim.xpage_slide_out_right);
                        finish();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(RelatedGoodsActivity.this, "网络请求失败", Toast.LENGTH_SHORT).show();
            }
        }) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                String sesseionid = new ConnectViaSession(RelatedGoodsActivity.this).GetSession();
                if (!sesseionid.equals("")) {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("cookie", sesseionid);
                    return headers;
                } else {
                    return super.getHeaders();
                }
            }
        };
        requestQueue.add(stringRequest);


    }


    @Override
    protected int getLayoutId() {
        return R.layout.bill_activity;

    }
    @Override
    protected int getStatusBarColor() {
        return R.color.white_easy_photos;
    }


    /**
     * 返回
     */
    private void relatedGoodsReturn() {
        related_goods_return = findViewById(R.id.related_goods_return);
        related_goods_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                overridePendingTransition(R.anim.xpage_slide_in_left,R.anim.xpage_slide_out_right);
                finish();
            }
        });
    }

}
